<?php

defined('__EAPI_BASE') or define('__EAPI_BASE', dirname(__FILE__));

class EAPICharWalletJournal extends EAPIxml{
	
	protected $journal;
	
	public function __Construct($apiCore,EAPIUser $uObj){
		try{

			parent::__Construct($apiCore);
			$this->journal = new SimpleXMLElement($this->getXML('/char/WalletJournal.xml.aspx',$uObj));

			if(empty($this->journal) || $this->journal == ""){
				throw new EAPIException('Cannot Get Character Wallet Journal',606.1);
			}

			if($this->apiCore['__APIDebug'] == 'characterWalletJournal'){

				print $this->journal;
			}
			
		}catch (EAPIException $e){
			print $e;
			die();
		}catch (Exception $e){
			print "<pre>".__CLASS__."[{$e->getCode()}]: {$e->getMessage()}\n</pre>";
			die();
		}
		
	}
	public function __Destruct(){}
	
	public function getJournal($offset = 0, $limit = false){

		//print $offset." ".$limit."<br/>";
		if(!$limit){
			$limit = count($this->journal->result->rowset->{'row'});
		}
		$journal[] = "<table class='walletJournal'>";
		for($i=$offset; $i<=$limit+$offset-1;$i++){
			//print $i." ".$limit."<br/>";
			$journal[] = $this->formatRow($this->journal->result->rowset->{'row'}[$i]);

		}
		$journal[] = "</table>";
		
		//print "<pre>";print_r($mailArr);print"</pre>";
		return implode($journal);
	}
	
	private function formatRow($row){
		try{
			/*
			 * @note - Structure of the row data. For formatting reference.
			 * 
			 * <row date="2010-09-16 02:24:00" refID="3264395808" refTypeID="38" 
			 * 		ownerName1="" ownerID1="2" ownerName2="W3iry" ownerID2="535127575" 
			 * 		argName1="Lateralus Perspicuus" argID1="1581004859" amount="3.20" 
			 * 		balance="23144.11" reason="" taxReceiverID="" taxAmount="" 
			 * 	/>
			 */ 
								
			/*
			 * @note - Need to impliment the official RefTypes List from the EVE API
			 * http://wiki.eve-id.net/APIv2_Eve_RefTypes_XML
			 */
			$refTypes = array(	
0 => "Undefined",1 => "Player Trading",2 => "Market Transaction",3 => "GM Cash Transfer",4 => "ATM Withdraw",
5 => "ATM Deposit",6 => "Backward Compatible",7 => "Mission Reward",8 => "Clone Activation",9 => "Inheritance",
10 => "Player Donation",11 => "Corporation Payment",12 => "Docking Fee",13 => "Office Rental Fee",14 => "Factory Slot Rental Fee",
15 => "Repair Bill",16 => "Bounty",17 => "Bounty Prize",18 => "Agents_temporary",19 => "Insurance",
20 => "Mission Expiration",21 => "Mission Completion",22 => "Shares",23 => "Courier Mission Escrow",24 => "Mission Cost",
25 => "Agent Miscellaneous",26 => "LP Store",27 => "Agent Location Services",28 => "Agent Donation",29 => "Agent Security Services",
30 => "Agent Mission Collateral Paid",31 => "Agent Mission Collateral Refunded",32 => "Agents_preward",33 => "Agent Mission Reward",
34 => "Agent Mission Time Bonus Reward",35 => "CSPA",36 => "CSPAOfflineRefund",37 => "Corporation Account Withdrawal",
38 => "Corporation Dividend Payment",39 => "Corporation Registration Fee",40 => "Corporation Logo Change Cost",
41 => "Release Of Impounded Property",42 => "Market Escrow",43 => "Agent Services Rendered",44 => "Market Fine Paid",
45 => "Corporation Liquidation",46 => "Brokers Fee",47 => "Corporation Bulk Payment",48 => "Alliance Registration Fee",
49 => "War Fee",50 => "Alliance Maintainance Fee",51 => "Contraband Fine",52 => "Clone Transfer",53 => "Acceleration Gate Fee",
54 => "Transaction Tax",55 => "Jump Clone Installation Fee",56 => "Manufacturing",57 => "Researching Technology",
58 => "Researching Time Productivity",59 => "Researching Material Productivity",60 => "Copying",61 => "Duplicating",
62 => "Reverse Engineering",63 => "Contract Auction Bid",64 => "Contract Auction Bid Refund",65 => "Contract Collateral",
66 => "Contract Reward Refund",67 => "Contract Auction Sold",68 => "Contract Reward",69 => "Contract Collateral Refund",
70 => "Contract Collateral Payout",71 => "Contract Price",72 => "Contract Brokers Fee",73 => "Contract Sales Tax",
74 => "Contract Deposit",75 => "Contract Deposit Sales Tax",76 => "Secure EVE Time Code Exchange",77 => "Contract Auction Bid (corp)",
78 => "Contract Collateral Deposited (corp)",79 => "Contract Price Payment (corp)",80 => "Contract Brokers Fee (corp)",
81 => "Contract Deposit (corp)",82 => "Contract Deposit Refund",83 => "Contract Reward Deposited",84 => "Contract Reward Deposited (corp)",
85 => "Bounty Prizes",86 => "Advertisement Listing Fee",87 => "Medal Creation",88 => "Medal Issued",89 => "Betting",
90 => "DNA Modification Fee",91 => "Sovereignity bill",92 => "Bounty Prize Corporation Tax",93 => "Agent Mission Reward Corporation Tax",
94 => "Agent Mission Time Bonus Reward Corporation Tax",95 => "Upkeep adjustment fee",96 => "Planetary Import Tax",
97 => "Planetary Export Tax",98 => "Planetary Construction");
	
			if(array_key_exists((int)$row['refTypeID'], $refTypes)){
				$refName = $refTypes[(int)$row['refTypeID']];
			}else{
				throw new EAPIException('refTypeID Not Found: '.(int)$row['refTypeID'],606.2);
			}
			
			// Date, Type, Ammount, Balance, Description
			return "<tr><td class='walletJournal-date'>".(string)$row['date']."</td>
						<td class='walletJournal-refTypeID'>".$refName."</td>
						<td class='walletJournal-amount'>".(string)$row['amount']."</td>
						<td class='walletJournal-balance'>".(string)$row['balance']."</td>
						<td class='walletJournal-argName1'>".(string)$row['argName1']."</td></tr>";
		}catch (EAPIException $e){
			print $e;
			die();
		}
		
	}
};